<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button onclick="createHome()">创建房间</button>
<br/>
<input id="homeNum" type="text"/>
<button onclick="joinHome()">请输入房间号加入房间</button>
</body>
<script language="javascript" type="text/javascript">
    const {session} = require('electron').remote;
    selectHome();

    function createHome() {
        let username2 = 999;
        let array = [];
        for(let i = 0; i < global.qqw.length; i++) {
            array.unshift(global.qqw[i].num);
        }
        let num = array.shift() + 1;
        session.defaultSession.cookies.get({ url: 'http://localhost/home.html' }, (error, cookies) => {
            addHome(num, cookies[0].name,username2)
            addGame(num,cookies[0].name,username2)
        });
        window.location.href='./game.html';
    }
    function joinHome() {
        let state = () => {
            let homeNum = document.getElementById('homeNum').value;
            for(let i = 0; i < global.qqw.length; i++) {
                if(homeNum == global.qqw[i].num){
                    return true;
                }
            }
            return false;
        };
        if(state()){
            let homeNum = document.getElementById('homeNum').value;
            session.defaultSession.cookies.get({ url: 'http://localhost/home.html' }, (error, cookies) => {
                username2 = cookies[0].name;
                for(let i = 0; i < global.qqw.length; i++) {
                    if (homeNum == global.qqw[i].num) {
                        update(homeNum, username2);
                        updateGame(homeNum, username2);
                    }
                }
            });
            window.location.href='./game.html';
        }
        else {
            alert('房间号错误');
        }
    }
    function addHome(num,username1,username2) {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });

        connection.connect();

        let addSql = 'INSERT INTO home(num,username1,username2) VALUES(?,?,?)';
        let addSqlParams = [];
        addSqlParams.push(num);
        addSqlParams.push(username1);
        addSqlParams.push(username2);

//增
        connection.query(addSql,addSqlParams,function (err, result) {
            if(err){
                console.log('[INSERT ERROR] - ',err.message);
                return;
            }
        });

        connection.end();

    }
    function addGame(num,username1,username2) {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });
        connection.connect();
        let addSql = 'INSERT INTO game(num,user1,user2) VALUES(?,?,?)';
        let addSqlParams = [];
        addSqlParams.push(num);
        addSqlParams.push(username1);
        addSqlParams.push(username2);
//增
        connection.query(addSql,addSqlParams,function (err, result) {
            if(err){
                console.log('[INSERT ERROR] - ',err.message);
                return;
            }
        });
        connection.end();
    }
    function selectHome() {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });
        connection.connect();
        let  sql = 'SELECT * FROM home';
        //查
        connection.query(sql,function (err, result) {
            if(err){
                console.log('[SELECT ERROR] - ',err.message);
                return;
            }
            global.qqw = result;
        });
        connection.end();
    }
    function update(num, username2) {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });
        connection.connect();
        let modSql = 'UPDATE home SET username2 = ? WHERE num = ?';
        let modSqlParams = [];
        modSqlParams.push(username2);
        modSqlParams.push(num);
//改
        connection.query(modSql,modSqlParams,function (err, result) {
            if(err){
                console.log('[UPDATE ERROR] - ',err.message);
                return;
            }
            console.log(result);
        });
        connection.end();
    }
    function updateGame(num, username2) {
        let mysql  = require('mysql');
        let connection = mysql.createConnection({
            host     : 'localhost',
            user     : 'root',
            password : 'yang1sheng',
            port: '3306',
            database: 'test',
        });
        connection.connect();
        let modSql = 'UPDATE game SET user2 = ? WHERE num = ?';
        let modSqlParams = [];
        modSqlParams.push(username2);
        modSqlParams.push(num);
//改
        connection.query(modSql,modSqlParams,function (err, result) {
            if(err){
                console.log('[UPDATE ERROR] - ',err.message);
                return;
            }
            console.log(result);
        });
        connection.end();
    }
</script>
</html>